import mitmproxy.http
from mitmproxy import ctx
import socket
BUFSIZE = 1024

ask_ip_port = ('127.0.0.1', 9999)
response_ip_port = ('127.0.0.1', 9998)

# 参考网址：https://www.cnblogs.com/c-x-a/p/9753526.html
class ExamplePlug:
    def __init__(self):
        self.client =socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

    def request(self, flow: mitmproxy.http.HTTPFlow):
        self.client.sendto(str(flow.request.url).encode(), ask_ip_port)

    def response(self, flow: mitmproxy.http.HTTPFlow):
        self.client.sendto(str(flow.response.status_code).encode(), response_ip_port)
        flow.response.set_text("hello  mitmproxy")

    def __del__(self):
        self.client.close()


